﻿using UnityEngine.Helper;

namespace UnityEngine.UI
{
    [AddComponentMenu("UI/DynamicRawImage")]
    public class DynamicRawImage : RawImage
    {
        public DynamicAtlasGroup mGroup = DynamicAtlasGroup.Size_1024;
        [SerializeField]
        public string spritePath = null;
        private DynamicAtlas m_Atlas;
        private OnCallBack onCallBack;
        private bool isSetImage = false;
        protected override void Start()
        {
            base.Start();
#if UNITY_EDITOR
            if (Application.isPlaying)
            {
                OnPreDoImage();
            }
#else
       OnPreDoImage();
#endif
        }

        void OnPreDoImage()
        {
            if (texture != null && isSetImage == false) //说明这个rawimage事先挂了texture，此时需要给copy到图集中
            {
                SetGroup(mGroup);
                PreSetImage();
            }
        }
        public void SetGroup(DynamicAtlasGroup group)
        {
            if (m_Atlas != null)
            {
                return;
            }
            mGroup = group;
            m_Atlas = DynamicAtlasManager.Instance.GetDynamicAtlas(group);
        }

        public DynamicAtlasGroup GetGroup()
        {
            return mGroup;
        }
        public void OnGetImageCallBack(Texture tex, Rect rect, string path)
        {
            this.texture = tex;
            uvRect = rect;
            gameObject.SetActiveVirtual(true);
            if (onCallBack != null)
            {
                onCallBack();
            }
        }
        public void OnGetMaterialCallBack(Material material, Rect rect, string path)
        {
            this.texture = null;
            this.material = material;
            uvRect = rect;
            gameObject.SetActiveVirtual(true);
            if (onCallBack != null)
            {
                onCallBack();
            }
        }

        private void PreSetImage()
        {
            if (string.IsNullOrEmpty(spritePath))
            {
                return;
            }
            if (AtlasConfig.kUsingCopyTexture)
            {
                m_Atlas.SetTexture(spritePath, texture, OnGetImageCallBack);
            }
            else
            {
                m_Atlas.SetTexture(spritePath, texture, OnGetMaterialCallBack);
            }

            //此时可以卸载自己的引用计数
        }
        public void SetImage(string path, OnCallBack callBack = null)
        {
            isSetImage = true;
            gameObject.SetActiveVirtual(false);
            SetImageNoHide(path, callBack);
        }
        public void SetImageNoHide(string path, OnCallBack callBack = null)
        {
            onCallBack = callBack;
            spritePath = path;
            if (m_Atlas == null)
            {
                SetGroup(mGroup);
            }
            if (AtlasConfig.kUsingCopyTexture)
            {
                m_Atlas.GetImage(path, OnGetImageCallBack);
            }
            else
            {
                m_Atlas.GetImage(path, OnGetMaterialCallBack);
            }
        }
        public void OnDispose()
        {
            if (string.IsNullOrEmpty(spritePath) == false && m_Atlas != null)
            {
                m_Atlas.RemoveImage(spritePath, false);
                //m_Atlas.RemoveImage(this._currentPath, true);
                spritePath = null;
            }
            isSetImage = false;
        }
        protected override void OnDestroy()
        {
            base.OnDestroy();
            OnDispose();
        }
    }
}
